from models import Cardio
from fiteat.utils import get_units, export_func, import_func, map_fields_func
from fiteat.chart import graph as graph_func
from django.utils.translation import ugettext as _

def export_view(request):
    data = Cardio.objects.filter(user=request.user).order_by('date')
    filename = 'cardio.csv'
    fields = ['date', 'workout_type_or_other', 'intensity', 'duration', 'distance', 'avg_hr', 'max_hr', 'calories', 'comment']
    return export_func(request, fields, data, filename)

FIELD_CHOICES = (
    ('-', _('---')),
    ('date', _('Date')),
    ('other_workout_type', _('Workout Type')),
    ('intensity', _('Intensity')),
    ('duration', _('Duration')),
    ('distance', _('Distance')),
    ('avg_hr', _('Average Heart Rate')),
    ('max_hr', _('Max Heart Rate')),
    ('calories', _('Calories')),
    ('comment', _('Comment')),
)

def import_view(request):
    return import_func(request, 'cardio/import.html', 'cardio/map_fields.html', FIELD_CHOICES)

def map_fields(request):
    return map_fields_func(request, 'cardio/map_fields.html', FIELD_CHOICES, Cardio, _('cardio sessions'), '/fiteat/admin/')

def graph(request, fields, period):
    prefs = request.user.get_profile().cardio_profile
    data = Cardio.objects.filter(user=request.user).order_by('date')
    # TODO: take it from the model!!!
    known_fields = {
        'duration': 'time',
        'distance': 'distance',
        'avg_hr': '',
        'max_hr': '',
        'calories': '',
        'custom1': 'custom',
        'custom2': 'custom',
        'custom3': 'custom',
        'custom4': 'custom',
        'custom5': 'custom',
    }
    return graph_func(request, fields, period, prefs, data, known_fields)
